import request from '@/utils/request'

/**
 *  默认搜索关键词
 *  @return {Object}
 */
export const getDefaultSearch = (params) => {
    return request.get('/search/default', params)
}

/**
 *  热门搜索 详细
 *  @return {Array}
 */
export const getHotDetailSearch = (params) => {
    return request.get('/search/hot/detail', params)
}

/**
 *  搜索建议
 *  @param {String} keywords
 *  @param {String} type
 *  @return {Array}
 */
export const getSuggestList = (params) => {
    let data = {
        ...params,
        type: 'mobile',
    }
    return request.get('/search/suggest', data)
}

/**
 *  搜索
 *  @param {String} keywords
 *  @return {Array}
 */
export const getSearchList = (params) => {
    return request.get('/cloudsearch', params)
}

/**
 *  根据id去获取音乐url
 *  @param  {Number} id
 *  @return {Array}
 */
export const getMusicUrl = (params) => {
    // return request.get('/song/url', params)
    return new Promise((resolve, reject) => {
        uni.request({
            url: uni.getStorageSync('ROOT_URL') + '/api/music/getMusicUrl',
            data: params,
            header: {
                'Accept': 'application/json',
                'Content-Type': 'application/json',
                '__userId': uni.getStorageSync('LOGIN_RESULT').loginCode,
                '__token': uni.getStorageSync('TOKEN')
            },
            method: "POST",
            success: (response) => {
                let res = response.data
                if (res.code === 200) {
                    resolve(res)
                } else if (res.code === 401) {
                    //请重新登录
                    uni.removeStorageSync('LOGIN_RESULT')
                    uni.reLaunch({
                        url: '/pages/login/index?statusCode=401'
                    })
                } else {
                    reject(res)
                    showError(res);
                }
            },
            fail: (error) => {
                if (error && error.response) {
                    reject(error.response)
                }
            },
        });
    })
}

/**
 *  判断是否有版权
 *  @param  {Number} id
 *  @return {Array}
 */
export const checkMusicUrl = (params) => {
    return request.get('/check/music', params)
}

/**
 *  根据id去获取歌词
 *  @param  {Number} id
 *  @return {Array}
 */
export const getLyric = (params) => {
    return request.get('/lyric', params)
}
